package licode;

public class L34 {
    public static int[] searchRange(int[] nums, int target) {
        int leftsearch = leftsearch(nums, target);
        int rightsearch= rightsearch(nums, target);
        if (leftsearch==-2 || rightsearch==-2){
            return new int[]{-1,-1};
        }
        if (rightsearch-leftsearch>1){
            return new int[]{leftsearch+1,rightsearch-1};
        }
        else return new int[]{-1,-1};

    }


    private static int leftsearch(int[] nums, int target) {
        int low=0;
        int high=nums.length-1;
        int leftBorder = -2;
        while (low<=high){
            int mid=(low+high)/2;
            if (nums[mid]>=target){
                high=mid-1;
                leftBorder=high;
            }
            if (nums[mid]<target){
                low=mid+1;
            }

        }
        return leftBorder;

    }
    private static int rightsearch(int[] nums, int target) {
        int low=0;
        int high=nums.length-1;
        int rightBorder=-2;
        while (low<=high){
            int mid=(low+high)/2;
            if (nums[mid]>target){
                high=mid-1;
            }
            if (nums[mid]<=target){
                low=mid+1;
                rightBorder=low;
            }
        }
        return rightBorder;

    }
    public static void main(String[] args) {
        int arr[]={5,7,8,8,10};
        int[] ints = searchRange(arr, 7);
        System.out.println(ints[0]);
        System.out.println(ints[1]);

    }

}
